//copyright (c) 2016 L.W.C<lwc@li.cm>

#include "NCvar.h"

int _function_comp_(void *ptrA,void *ptrB){
	char *a;
	char *b;
	if(ptrB==0||ptrA==0){
		return 0;
	}
	a=((function*)ptrB)->name;
	b=(char*)ptrA;
	while(*a){
		if(!*b||*a!=*b)return 0;
		a++;b++;
	}
	if(*b)return 0;
	return 1;
}

mrulist* MRU_Push(mrulist *m,void *ptr){
	mrulist *new;
	new=(mrulist*)vxin_malloc(sizeof(mrulist));
	new->ptr=ptr;
	new->next=m;
	return new;
}

mrulist* MRU_Pop(mrulist *m,void **ptr){
	mrulist *old;
	*ptr=m->ptr;
	old=m;
	m=m->next;
	vxin_free(old);
	return m;
}

mrulist* MRU_loopup(mrulist *m,void *ptr,int (*fun)(void* scr,void* list)){
	mrulist *last,*start;
	last=m;
	start=m;
	while(m&&!fun(ptr,m->ptr)){
		last=m;
		m=m->next;
	}
	if(fun(ptr,m->ptr)){
		last->next=m->next;
		m->next=start;
		return m;
	}
	else
	{
		return m;
	}
}

void Call_Fun(NC *nc,char *name){
	nc->funreg=MRU_loopup(nc->funreg,(void*)name,&_function_comp_);
	((function*)nc->funreg->ptr)->u.fun(nc);
}
